class CommentsController < ApplicationController
  before_filter :account_required, :login_required, :find_user
  
  def create
    @comment = Comment.new(params[:comment])
    @comment.commenter_id = current_user.id
    @comment.user_id = @user.id
    if @comment.save
      flash[:notice] = "Comment has been created"
      redirect_to user_path(@user)
    else
      flash[:error] = "Could not created a comment"
      redirect_to @user
    end
  end
  
  # Destroy a comment
  def destroy
    @comment = Comment.find(params[:id])
    if @comment.destroy
      flash[:notice] = "Comment has been deleted."
      redirect_to user_path(@user)
    else
      flash.now[:error] = "Could not delete a comment."
      render user_path(@user)
    end
  end
  
  protected
  
  def find_user
    @user = User.find_by_id(params[:user_id], :conditions => ['company_id = ?', current_account.id])
    raise ActiveRecord::RecordNotFound, "Page not found" if @user.nil?
  end
end
